import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home',
    meta: {
      requireLogin: false
    }
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home/index'),
    meta: {
      requireLogin: false
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index'),
    meta: {
      requireLogin: false
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/register/index'),
    meta: {
      requireLogin: false
    }
  },
  {
    path: '/details',
    name: 'details',
    component: () => import('../views/details/index'),
    meta: {
      requireLogin: true
    }
  },
  {
    path: '/profile',
    name: 'profile',
    component: () => import('../views/profile/index'),
    meta: {
      requireLogin: true
    }
  },
  {
    path: '/release',
    name: 'release',
    component: () => import('../views/release/index'),
    meta: {
      requireLogin: true
    }
  },
  {
    path: '/setting',
    name: 'setting',
    component: () => import('../views/setting/index'),
    meta: {
      requireLogin: true
    }
  },
]

const router = new VueRouter({
  routes,
  mode: 'history',
  // baseURL:'v1'
})


router.beforeEach(((to, from, next) => {
  if (to.meta.requireLogin) {
    if (sessionStorage.getItem('token')) {
        next()
    } else {
      next({name: 'login'})
    }
  } else {
    next()
  }
}))

export default router
